package org.example.springmvcdemo.controller;

import org.example.springmvcdemo.model.Student;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
//RequestMapping 既可以修饰类 也可以修饰方法
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/hello")
    public String say(){
        return "hello,MARS";
    }
    @RequestMapping("/r1")
    public String r1(String name){
        return "接收到参数，name:"+name;
    }
    @RequestMapping("/r2")
    public String r2(Integer age){
        return "接收到参数，age:"+age;
    }
    @RequestMapping("/r3")
    public String r3(int age){
        return "接收到参数，age:"+age;
    }
    @RequestMapping("/r4")
    public String r3(String name,Integer age){
        return "接收到参数，name:"+ name+" age:"+age;
    }
    @RequestMapping("/r5")
    public String r5(Student student){
        return "接收到参数，student:"+student;
    }
    //参数重命名 接受前段一个参数 赋值给谁 name前端 userName后端
    //前端传userName会报错
    //@RequestParam 是一个必传参数
    @RequestMapping("/r6")
    public String r6(@RequestParam("name") String userName){
        return "接收到参数，name:"+userName;
    }
    @RequestMapping("/r7")
    public String r7(@RequestParam(value="name",required = false) String userName){
        return "接收到参数，name:"+userName;
    }
    @RequestMapping("/r8")
    public String r8(String[] array){
        return "接收到参数，array:"+ Arrays.toString(array);
    }
    @RequestMapping("/r9")
    public String r9(@RequestParam(required = false) List<String> list){
        return "接收到参数，list:"+ list;
    }
    /*
    * 接收json
    * */
    @RequestMapping("/r10")
    public ResponseEntity< Student> r10( @RequestBody Student student){
        return ResponseEntity.ok(student);
    }
    /*@RequestMapping("/r10")
    public String r11(Student student){
        return "接收到参数，student:"+student;
    }*/

    //获取URL中的参数@PathVariable
    @RequestMapping("/article/{articleId}/{name}")
    public String r11(@PathVariable("articleId") Integer articleId,@PathVariable("name")String name){
        return "接收到参数，articleId:"+articleId+" name:"+name;
    }
    /*
    * 上传文件
    * */
    @RequestMapping("/r13")
    public String r13(@RequestPart("file") MultipartFile pdfFile){
        String originalFilename = pdfFile.getOriginalFilename();
        return "接收到文件，文件名称:"+originalFilename;
    }
}
